$(document).ready(function(){
    getContent()
    $('.menu_item').click(function() {
        $('.menu_item').removeClass('menu_item_select');
        $(this).addClass('menu_item_select');
        $('.menu_item').not(this).removeClass('menu_item_select');
    });
    $('.menu_box_item').click(function () {
        $('.menu_box_item').removeClass('menu_box_item_select');
        $(this).addClass('menu_box_item_select');
        $('.menu_box_item').not(this).removeClass('menu_box_item_select');
    });
    $('.unfold_all').click(function () {
        $('.menu_box_item').removeClass('menu_box_item_select');
        $('.menu_box_item').addClass('menu_box_item_select');
    })
});
function getContent() {
    var loadIndex = layer.load(2);
    let datas = {
        url: '/api/page',
        http_type: 'POST',
        datas: {
            "relate_id": 134, //分类ID
            "keyset": "hpbs_methods" //固定值
        }
    }
    wxx.HttpReqeust(datas, function (response) {
        layer.close(loadIndex)
        // response = {
        //     "code": 1,
        //     "msg": "获取成功",
        //     "time": "1720111568",
        //     "data": {
        //         "title": "FASD",
        //         "create_time": "2024-07-04 21:12:59",
        //         "author": "",
        //         "contents": [
        //             {
        //                 "id": 1,
        //                 "title": "FSDAS",
        //                 "parent_id": 0,
        //                 "weight": 0,
        //                 "content": "FSDAS内容<br />",
        //                 "page_id": 3,
        //                 "children": [
        //                     {
        //                         "id": 2,
        //                         "title": "FWEW",
        //                         "parent_id": 1,
        //                         "weight": 2,
        //                         "content": "FWEW内容",
        //                         "page_id": 3,
        //                         "children": []
        //                     }
        //                 ]
        //             }
        //         ]
        //     }
        // }
        // http://127.0.0.1:5500/auxiliary/html/survey.html
        if (response.code == 1) {
            if(
                response.data.contents.length==0
            ){
            $('.empty').html(emptyHtml);
            return
            }
            

            $("#page_title").text(response.data.title)
            $("#create_time").text(response.data.create_time)
            $("#author").text('发布者：' + response.data.author)
            $(".left_menu_box").children(".menu_item").remove();
            for (let i = 0; i < response.data.contents.length; i++) {
                let menu_item = ''
                if(i==0){
                    menu_item = $("<div class='menu_item menu_item_select'></div>")
                }else {
                    menu_item = $("<div class='menu_item'></div>")
                }
                $(menu_item).attr('data-index',i);
                let menu_item_out = $("<div class='menu_item_out'></div>")
                let menu_item_out_span = $("<span></span>")
                $(menu_item_out_span).text(response.data.contents[i].title)
                let menu_item_out_img = $("<img src='../../icons/icon_menu.png'/>")
                $(menu_item_out).append(menu_item_out_img)
                $(menu_item_out).append(menu_item_out_span)
                let menu_item_drop = $("<div class='menu_item_drop'></div>")
                /****/
                let article_box_item = $("<div class='article_box_item'></div>")
                let article_box_title = $("<div class='article_box_title'></div>")
                let div_1 = $("<div></div>")
                let div_1_img = $("<img src='../../icons/icon_article_unfold.png'/>")
                let div_1_span = $("<span></span>")
                $(div_1_span).text(response.data.contents[i].title)
                $(div_1).append(div_1_img)
                $(div_1).append(div_1_span)
                let div_2 = $("<div></div>")
                let div_2_img = $("<img src='../../icons/icon_add.png'/>")
                let div_2_span = $("<span style='margin-left: 5px'></span>")
                $(div_2_span).text("新增")
                let div_3_span = $("<span style='color: #C7C7C7'></span>")
                $(div_3_span).html('&nbsp;'+'&nbsp;'+'|'+'&nbsp;'+'&nbsp;')
                let div_4_img = $("<img src='../../icons/icon_error_correction.png'/>")
                let div_5_span = $("<span style='margin-left: 5px'></span>")
                $(div_5_span).text("纠错")
                $(div_2).append(div_2_img)
                $(div_2).append(div_2_span)
                $(div_2).append(div_3_span)
                $(div_2).append(div_4_img)
                $(div_2).append(div_5_span)
                $(article_box_title).append(div_1)
                $(article_box_title).append(div_2)
                $(article_box_item).append(article_box_title)
                let article_box_content = $("<div class='article_box_content'></div>")
                $(article_box_content).html(response.data.contents[i].content)
                $(article_box_item).append(article_box_content)
                $('.article_box').append(article_box_item)
                /****/
                for (let j = 0; j < response.data.contents[i].children.length; j++) {
                    let menu_item_drop_span = $("<span class='menu_item_drop_item'></span>")
                    $(menu_item_drop_span).attr('data-index',i)
                    $(menu_item_drop_span).attr('data-child',j)
                    $(menu_item_drop_span).text(response.data.contents[i].children[j].title)
                    $(menu_item_drop).append(menu_item_drop_span)
                    /****/
                    let article_box_item = $("<div class='article_box_item'></div>")
                    let article_box_title = $("<div class='article_box_title'></div>")
                    let div_1 = $("<div></div>")
                    let div_1_img = $("<img src='../../icons/icon_article_unfold.png'/>")
                    let div_1_span = $("<span></span>")
                    $(div_1_span).text(response.data.contents[i].title)
                    $(div_1).append(div_1_img)
                    $(div_1).append(div_1_span)
                    let div_2 = $("<div></div>")
                    let div_2_img = $("<img src='../../icons/icon_add.png'/>")
                    let div_2_span = $("<span style='margin-left: 5px'></span>")
                    $(div_2_span).text("新增")
                    let div_3_span = $("<span style='color: #C7C7C7'></span>")
                    $(div_3_span).html('&nbsp;'+'&nbsp;'+'|'+'&nbsp;'+'&nbsp;')
                    let div_4_img = $("<img src='../../icons/icon_error_correction.png'/>")
                    let div_5_span = $("<span style='margin-left: 5px'></span>")
                    $(div_5_span).text("纠错")
                    $(div_2).append(div_2_img)
                    $(div_2).append(div_2_span)
                    $(div_2).append(div_3_span)
                    $(div_2).append(div_4_img)
                    $(div_2).append(div_5_span)
                    $(article_box_title).append(div_1)
                    $(article_box_title).append(div_2)
                    $(article_box_item).append(article_box_title)
                    let article_box_content = $("<div class='article_box_content'></div>")
                    $(article_box_content).html(response.data.contents[i].content)
                    $(article_box_item).append(article_box_content)
                    $('.article_box').append(article_box_item)
                    /****/
                }
                $(menu_item).append(menu_item_out)
                $(menu_item).append(menu_item_drop)
                $(".left_menu_box").append(menu_item)
                $("#right_detail_title").text(response.data.contents[i].title)
                $(".right_detail_content").text(response.data.contents[i].content)
                $(document).on('click', '.menu_item', function() {
                    let index = $(this).attr("data-index");
                    $("#right_detail_title").text(response.data.contents[index].title)
                    $(".right_detail_content").text(response.data.contents[index].content)
                    $('.menu_item').removeClass('menu_item_select');
                    $(this).addClass('menu_item_select');
                    $('.menu_item').not(this).removeClass('menu_item_select');
                });
                $(document).on('click', '.menu_item_drop_item', function(e) {
                    e.stopPropagation();
                    let index = $(this).attr("data-index");
                    let childIndex = $(this).attr("data-child");
                    $("#right_detail_title").text(response.data.contents[index].children[childIndex].title)
                    $(".right_detail_content").text(response.data.contents[index].children[childIndex].content)
                });

            }
        } else {
            layer.alert(response.msg, {title: '温馨提示'});
        }
    });
}
